Thread: if (key [KEY_ESC])

  1. #1
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72

    if (key [KEY_ESC])

    Hi guys.

    I've got one question with allegro and the function key([//Name of the key])

    I want that, if the user press ENTER, the program continues with the execution, but if the user press ESC, the program finishes.

    The code I'm using is:

    Code:
        printf("Press Enter to continue or ESC to exit...\n");
        do{
            	if (keypressed()) { 
                //ENTER
                	if (key[KEY_ENTER]) {  
               		    llegeix(jRanking); 
                        tauler(cMode,jugador, nJugadors,casella);
                        joc(jugador, nJugadors, casella, jRanking); 
                        nOk = 1;
                        
                    }  
                    //ESC..  
                    if (key[KEY_ESC]) {   
                        printf("FINS AVIAT!\n");
                        nOk = 1;
                        exit(1); 
                        
                    } 
                    //like fflush() in allegro. 
                    clear_keybuf();  
                }
    	}while(!nOk);
    The problem here is that nothing happens. The printf appears but if I press any key, nothing happens.

    I was trying to find the error but I didn't find it
    Can you help me, please?

    If you need more code because you think that the error is not here, just ask me for it.

    Thank you guys!

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    i know i need more, as keypressed is unknown to me.....

    headers?

    i do know there was kbhit() in conio.h, but i know that is a "garbage" heade now!

  3. #3
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Ok Crossfire, here is the rest of the code. It's part of the main.

    Code:
    #include "tipus.h"
    int main(void) 
    {
        int i = 0;
        int nCorrecte = 0;
        int nJugadors = 0;
        char cMode;
        int nDaus[4];
        int error;
        int nRes = 0, nOk = 0;
        stPlayer jugador[4];
        stCasella casella[97];
        stDaus astDaus[4];
        stPlayer jRanking[5];
        
        srand(time(NULL));
     
     
     //Inicialització llibreria allegro 
    allegro_init(); 
    //Inicialització teclat 
    install_keyboard(); 
    //Inicialització entorn grafic, finestra 800*600 
    error=set_gfx_mode(GFX_SAFE, 800, 600, 0, 0); 
    if (error==0){ //Entrar codi aquí!
        while(nCorrecte == 0){
            printf("Mode de joc <E o F>: ");
            fflush(stdout);
            nRes = scanf("%c",&cMode);
     
            cMode = (char) tolower(cMode); 
            if(nRes == 1 && ( cMode == 'e' || cMode == 'f')){
                nCorrecte = 1;
            }
            else{
                printf("Error! Mode de joc incorrecte!\n");
            }
            getchar(); 
        }
        nCorrecte = 0;
        while(nCorrecte == 0){
            printf("\nNombre de jugadors: ");
            fflush(stdout); 
            nRes = scanf("%d",&nJugadors);
            if(nRes == 1 && (nJugadors >= 2 && nJugadors <= 4) ){
                nCorrecte = 1;
            }
            else{
                printf("Error, el nombre de jugadors ha d'estar entre 2 i 4.\n");
            }
     
            
            getchar(); 
        }
     
        
        for( i = 0; i < nJugadors; i++){
            printf("\nNom Jugador %d:  ", i+1);
            if(fgets(jugador[i].strNom, sizeof(jugador[i].strNom),stdin) != NULL) 
            {
                nRes = strlen(jugador[i].strNom);
                if(nRes > 0 && jugador[i].strNom[nRes-1] == '\n')
                    jugador[i].strNom[nRes-1] = '\0';
                    jugador[i].nColorfitxa = 2 + i;
                    jugador[i].nCasella = 0;
            }
        }
     
        printf("Daus de sortida: \n");
     
        for(i = 0; i < nJugadors; i++){
            astDaus[i].nDaus = rand() % 6+1;
            astDaus[i].nOrdre = i;
            printf("%d.%s - %d de 6\n",i+1,jugador[i].strNom,astDaus[i].nDaus);
        }
        
        
            ordenacio(jugador, astDaus, nJugadors);
     
        printf(" Enter to continue o ESC to exit...\n");
        do{
            	if (keypressed()) { 
                //ENTER...
                	if (key[KEY_ENTER]) {  
               		    llegeix(jRanking); 
                        tauler(cMode,jugador, nJugadors,casella);
                        joc(jugador, nJugadors, casella, jRanking); 
                        nOk = 1;
                        
                    }  
                    //ESC..  
                    if (key[KEY_ESC]) {   
                        printf("FINS AVIAT!\n");
                        nOk = 1;
                        exit(1); 
                        
                    } 
                    //like fflush() in allegro. 
                    clear_keybuf();  
                }
    		}while(!nOk); 
        
        llegeix(jRanking); 
        tauler(cMode,jugador, nJugadors,casella);
        for(i = 0; i < nJugadors; i++){
            circlefill(screen, 610, 150 + (20*i),6, jugador[i].nColorfitxa);
            circle(screen, 610, 150 + (20*i),6, jugador[i].nColorfitxa);
            textprintf_ex(screen, font, 645, 150 + (20*i), 0, 14, "%s", jugador[i].strNom);
        }
        joc(jugador, nJugadors, casella, jRanking); 
         
        
    }
    else{ 
    allegro_message("No es pot crear la finestra\n%s\n",allegro_error); 
    printf("No es pot crear la finestra\n%s\n",allegro_error); 
    } 
    //Close allegro 
    system("pause"); 
    allegro_exit(); 
    }  
    END_OF_MAIN();
    The header "tipus.h" includes "allegro.h".

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You haven't given any useful additional information, since there is no information about what keypressed() does or what the array key is.

    My guess is that you have misinterpreted the documentation for the library you're using.

    Does your code even compile? If it doesn't, it certainly will not execute.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Not only have you not bothered to give a link to the Allegro documentation, you haven't even specified which version you're using.

    At any rate, note under the documentation for install_keyboard() it says:
    "Once you have set up the Allegro handler, you can no longer use operating system calls or C library functions to access the keyboard."

    But you're using getchar() and fgets() after the call to install_keyboard().

    EDIT: Actually, try moving
    clear_keybuf(); to before the loop.

    Last edited by oogabooga; 08-28-2013 at 07:20 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by grumpy View Post
    You haven't given any useful additional information, since there is no information about what keypressed() does or what the array key is.

    My guess is that you have misinterpreted the documentation for the library you're using.

    Does your code even compile? If it doesn't, it certainly will not execute.
    keypressed() returns an int and the int means if a key was pressed or not.
    The array key is an array contained in the library allegro, if I'm not wrong.

    Quote Originally Posted by oogabooga View Post
    Not only have you not bothered to give a link to the Allegro documentation, you haven't even specified which version you're using.

    At any rate, note under the documentation for install_keyboard() it says:
    "Once you have set up the Allegro handler, you can no longer use operating system calls or C library functions to access the keyboard."

    But you're using getchar() and fgets() after the call to install_keyboard().

    EDIT: Actually, try moving
    clear_keybuf(); to before the loop.

    So, If I use install_keyboard(), since then, I cannot read anything like gets, scanf....etc, correct?

    But only in that function or on the rest of the program?

    For example, If I create another function called keyboard() and the code is:

    Code:
    int keyboard(){
        
    //Installing the keyboard
    install_keyboard();
    
    
    int nSortida = 0, nOk = 0;
    
    
        do{
              if (keypressed()) { 
                //ENTER...
                  if (key[KEY_ENTER]) {  
                    nSortida = 0; 
                    nOk = 1;
                        
               }  
                    //ESC..  
               if (key[KEY_ESC]) {   
                   nSortida = 1;
                   nOk = 1;
                   
                        
               } 
                    //like fflush() in allegro. 
                  clear_keybuf();
            }
        }while(!nOk);   
        
        return nSortida;
    }
    And I call this function more times, will it work? Or I cannot install the keyboard (call the function install_keyboard()) more than one time?
    Last edited by catasturslykid; 08-28-2013 at 09:23 AM.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    After a quick look at the documentation 'keypressed()' doesn't return or read any key value.

    keypressed() simply tells you if a key is/was pressed since the last call to readkey(). You have to call readkey() to actually read the key that was pressed then you compare it to the value such as KEY_ENTER (which is not part of an array)

    A simple example without any initialization or anything:
    Code:
    if (keypressed()) {
      int key;
      key = readkey();
    
      switch(key) {
         case KEY_ESC:
         /* ESC pushed */
         break;
         case KEY_ENTER:
          /* enter pushed */
         break;
         default:
         /* unhandled key */
         break;
      }
    }

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by nonpuz View Post
    After a quick look at the documentation
    Possibly too quick a look. His original loop should work (except as I've already mentioned he should move the clear_keybuf() to before the loop). Your replacement code would do nothing if a key wasn't being pressed at the moment keypressed() is executed.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed